package com.wangs.stock.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wangs.stock.entity.Stock;
import com.wangs.stock.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stock")
public class StockContoller {

    @Autowired
    private IStockService stockService;

    @RequestMapping("/reduce")
    public String reduce(@RequestParam("productId") Long productId) {

        QueryWrapper<Stock> queryWrapper = new QueryWrapper();
        Stock stock = stockService.getOne(queryWrapper.eq("product_id", productId).last("limit 1"));
        Integer count = stock.getCount();


        UpdateWrapper<Stock> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("product_id", productId);
        Stock stock1 = new Stock();
        stock1.setCount(count - 1);

        boolean update = stockService.update(stock1, updateWrapper);
        return "扣减库存成功！";
    }
}
